package com.shop2cn.iapi.sqoptrade.testcase;

import java.math.BigDecimal;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.shenqisettlement.parameter.bean.AgentProductBean;
import com.shop2cn.iapi.sqoptrade.parameter.enums.BizOperateLogEnum;
import com.shop2cn.iapi.sqoptrade.parameter.enums.UserTypeEnum;
import com.shop2cn.iapi.sqoptrade.parameter.req.ChangeOrderAddressReq;
import com.shop2cn.iapi.sqoptrade.parameter.utill.Utils;
import com.shop2cn.iapi.sqoptrade.resp.TradingServiceResp;
import com.shop2cn.iapi.sqoptrade.service.ChangeOrderAddressCall;
import com.ymatou.common.lang.CipherUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.SQPayGateWayCallService;
import com.ymttest.business.service.shenqi.DGSQBuildProxyChainCallService;
import com.ymttest.business.service.shenqi.SqTradingCallService;
import com.ymttest.database.sharding.db.ext.model.OrderExtPo;
import com.ymttest.database.sharding.db.sharding.model.OrderBizLogPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
import com.ymttest.database.sharding.db.sqlwapper.OrderWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;

/**
 * 修改收货人地址 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_SqChangeOrderAddress {
	
	private static ChangeOrderAddressReq coabean;
	private static ChangeOrderAddressCall coacall;
	private static OrderWapper orderWapper = new OrderWapper();
	
	private static long autotest_goodsOwnerId = Long.valueOf(EnvSetup
			.getData("autotest_goodsOwnerId"));
	
	private static OrderShardingWapper orderShardingWapper =  new OrderShardingWapper();
	
	String key = "123456789";
	String phoneKey = "123456789";
	String addreeKey = "123456789";

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("修改收货人地址-神器");
	}

	@Before
	public void caseUp() {

		coabean = new ChangeOrderAddressReq();
		coacall = new ChangeOrderAddressCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(coacall.getOpurl());
		orderShardingWapper.activeDeadCommunicationsLink(orderShardingWapper);
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@SuppressWarnings("static-access")
	private void verify(OrderExtPo orderExtPo) throws Exception {

		long orderId = coabean.getOrderId();
		
		Logger.comment(String.format("校验订单扩展表(order_ext) orderId:%s", orderId));

		orderWapper.sqlSession.clearCache();

		OrderExtPo actualOrderExt = orderWapper
				.selectOrderExtByOrderId(orderId);

		Logger.verifyEquals(true,
				Utils.calcTimeSpanMin(actualOrderExt.getUpdateTime()) < 2,
				"验证数据库UpdateTime跟系统当前时间差为2分钟内");

		Logger.verifyEquals(orderId, actualOrderExt.getOrderId(),
				"验证数据库OrderId");
		
		Logger.verifyEquals(
				CipherUtil.encryptAES(addreeKey, coabean.getAddress()),
				actualOrderExt.getUserAddress(), "验证数据库userAddress");
		
		Logger.verifyEquals(
				CipherUtil.decryptAES(addreeKey, actualOrderExt.getUserAddress()),
				coabean.getAddress(), "验证数据库userAddress解密后的数据");

		Logger.verifyEquals(true,
				Utils.calcTimeSpanMin(actualOrderExt.getUpdateTime()) < 2,
				"验证数据库UpdateTime跟系统当前时间差为2分钟内");

		Logger.verifyEquals(orderId, actualOrderExt.getOrderId(),
				"验证数据库OrderId");

		Logger.verifyEquals(coabean.getPostCode(),
				actualOrderExt.getPostCode(), "验证数据库PostCode");

		Logger.verifyEquals(
				CipherUtil.encryptAES(phoneKey,
						Utils.trimAndLeft(coabean.getPhone(), 20)),
				actualOrderExt.getUserPhone(), "验证数据库userPhone");
		
		Logger.verifyEquals(
				CipherUtil.decryptAES(phoneKey,
						actualOrderExt.getUserPhone()),
						Utils.trimAndLeft(coabean.getPhone(), 20), "验证数据库userPhone解密后的数据");

		Logger.verifyEquals(Utils.trimAndLeft(coabean.getReceievePerson(), 50),
				actualOrderExt.getReceiver(), "验证数据库Receiver");

		//订单主状态操作日志表(order_status_log)
		OrderBizLogPo orderBizLogPo = orderShardingWapper
				.selectOrderBizLogByOrderId(orderId);

		Logger.comment("验证订单业务操作记录表(order_biz_log)");

		String logContent = String.format("原收件人:{%s},手机号:{%s},邮编:{%s}地址:{%s}客服:{null}",
				orderExtPo.getReceiver(), orderExtPo.getUserPhone(),
				orderExtPo.getPostCode(), orderExtPo.getUserAddress());

		Logger.verifyEquals(logContent, orderBizLogPo.getContent(),
				"验证数据库Content");

		Logger.verifyEquals(orderId, orderBizLogPo.getOrderId(), "验证数据库OrderId");

		Logger.verifyEquals(String.valueOf(orderId), orderBizLogPo.getBizNo(),
				"验证数据库BizNo");
		Logger.verifyEquals(BizOperateLogEnum.CHANGE_ORDER_ADDRESS.toString(),
				orderBizLogPo.getBizType(), "验证数据库BizType");

		Logger.verifyEquals(coabean.getUserId(), orderBizLogPo.getOperator(),
				"验证数据库Operator");

	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeOrderAddress_001() {
		Logger.start(true, "订单状态为待付款,买家修改收货人地址成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder1(productBean, autotest_goodsOwnerId, 2);
			
			Thread.sleep(3000);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long userId = placeOrderResp.getUserId();

			Logger.comment("前置数据准备結束");

			OrderExtPo orderExtPo = orderWapper
					.selectOrderExtByOrderId(orderId);

			// 设置参数
			String address = "南京路步行街";
			coabean.setAddress(address);

			coabean.setOrderId(orderId);
			String phone = 139 + TestDataManager.getRandomNum(8);
			coabean.setPhone(phone);
			String postCode = "986574";
			coabean.setPostCode(postCode);
			String receievePerson = "洋码头";
			coabean.setReceievePerson(receievePerson);

			coabean.setUserId(userId);
			coabean.setUserType(UserTypeEnum.Normal);
			// 发送请求
			coacall.setData(coabean);
			coacall.callService();
			Logger.verifyEquals(ECode.SUCCESS, coacall.getCode(), "验证Code");

			// 验证数据库
			verify(orderExtPo);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeOrderAddress_002() {
		Logger.start(true, "订单状态为已付款，买家修改收货人地址成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder1(productBean, autotest_goodsOwnerId, 2);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long userId = placeOrderResp.getUserId();
			
			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId), placeOrderResp
							.getPayableAmount().subtract(BigDecimal.ZERO)
							.toString(), (int)userId,
					BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);

			Logger.comment("前置数据准备結束");

			OrderExtPo orderExtPo = orderWapper
					.selectOrderExtByOrderId(orderId);

			// 设置参数
			String address = "南京路步行街";
			coabean.setAddress(address);

			coabean.setOrderId(orderId);
			String phone = 139 + TestDataManager.getRandomNum(8);
			coabean.setPhone(phone);
			String postCode = "986574";
			coabean.setPostCode(postCode);
			String receievePerson = "洋码头";
			coabean.setReceievePerson(receievePerson);

			coabean.setUserId(userId);
			coabean.setUserType(UserTypeEnum.Normal);
			// 发送请求
			coacall.setData(coabean);
			coacall.callService();
			Logger.verifyEquals(ECode.SUCCESS, coacall.getCode(), "验证Code");

			// 验证数据库
			verify(orderExtPo);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeOrderAddress_003() {
		Logger.start(true, "订单状态为已接单，买家修改收货人地址成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder1(productBean, autotest_goodsOwnerId, 2);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long userId = placeOrderResp.getUserId();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId), placeOrderResp
							.getPayableAmount().subtract(BigDecimal.ZERO)
							.toString(), (int) userId,
					BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);
			
			SqTradingCallService.sellerAcceptOrder(orderId);

			Logger.comment("前置数据准备結束");
			
			OrderExtPo orderExtPo = orderWapper
					.selectOrderExtByOrderId(orderId);

			// 设置参数
			String address = "南京路步行街";
			coabean.setAddress(address);

			coabean.setOrderId(orderId);
			String phone = 139 + TestDataManager.getRandomNum(8);
			coabean.setPhone(phone);
			String postCode = "986574";
			coabean.setPostCode(postCode);
			String receievePerson = "洋码头";
			coabean.setReceievePerson(receievePerson);

			coabean.setUserId(userId);
			coabean.setUserType(UserTypeEnum.Normal);
			// 发送请求
			coacall.setData(coabean);
			coacall.callService();

			Logger.verifyEquals(ECode.SUCCESS, coacall.getCode(), "验证Code");

			// 验证数据库
			verify(orderExtPo);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeOrderAddress_004() {
		Logger.start(true, "订单状态为待付款,货头修改收货人地址失败");
		try {
			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder1(productBean, autotest_goodsOwnerId, 2);
			
			Thread.sleep(3000);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();

			Logger.comment("前置数据准备結束");

			// 设置参数
			String address = "南京路步行街";
			coabean.setAddress(address);

			coabean.setOrderId(orderId);
			String phone = "13200000005";
			coabean.setPhone(phone);
			String postCode = "986574";
			coabean.setPostCode(postCode);

			String receievePerson = "洋码头";
			coabean.setReceievePerson(receievePerson);

			coabean.setUserId(autotest_goodsOwnerId);
			coabean.setUserType(UserTypeEnum.Seller);
			// 发送请求
			coacall.setData(coabean);
			coacall.callService();

			Logger.verifyEquals(ECode.FAILED201, coacall.getCode(), "验证Code");
			Logger.verifyEquals(ECode.FAILED201_MSG_14, coacall.getMsg(),
					"验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeOrderAddress_005() {
		Logger.start(true, "订单状态为已接单，货头修改收货人地址成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");


			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder1(productBean, autotest_goodsOwnerId, 2);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long userId = placeOrderResp.getUserId();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId), placeOrderResp
							.getPayableAmount().subtract(BigDecimal.ZERO)
							.toString(), (int) userId,
					BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);
			
			SqTradingCallService.sellerAcceptOrder(orderId);

			Logger.comment("前置数据准备結束");

			OrderExtPo orderExtPo = orderWapper
					.selectOrderExtByOrderId(orderId);
			// 设置参数
			String address = "南京路步行街";
			coabean.setAddress(address);

			coabean.setOrderId(orderId);
			String phone = "13200000005";
			coabean.setPhone(phone);
			String postCode = "986574";
			coabean.setPostCode(postCode);

			String receievePerson = "洋码头";
			coabean.setReceievePerson(receievePerson);

			coabean.setUserId(autotest_goodsOwnerId);
			coabean.setUserType(UserTypeEnum.Seller);
			// 发送请求
			coacall.setData(coabean);
			coacall.callService();
			Logger.verifyEquals(ECode.SUCCESS, coacall.getCode(), "验证Code");
			// 验证数据库
			verify(orderExtPo);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeOrderAddress_007() {
		Logger.start(true, "订单不存在，修改订单地址失败");
		try {
			// 设置参数
			String address = "南京路步行街";
			coabean.setAddress(address);

			long orderId = 132314214;
			coabean.setOrderId(orderId);
			String phone = "13200000005";
			coabean.setPhone(phone);
			String postCode = "986574";
			coabean.setPostCode(postCode);

			String receievePerson = "洋码头";
			coabean.setReceievePerson(receievePerson);

			coabean.setUserId(288484);
			coabean.setUserType(UserTypeEnum.Seller);
			// 发送请求
			coacall.setData(coabean);
			coacall.callService();
			Logger.verifyEquals(ECode.FAILED201, coacall.getCode(), "验证Code");
			Logger.verifyIsContains(ECode.FAILED201_MSG_11, coacall.getMsg(),
					"验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ChangeOrderAddress_008() {
		Logger.start(false, "订单号为null,修改订单地址失败");
		try {
			// 设置参数
			String address = "南京路步行街";
			coabean.setAddress(address);

			//coabean.setOrderId(orderId);
			String phone = "13200000005";
			coabean.setPhone(phone);
			String postCode = "986574";
			coabean.setPostCode(postCode);

			String receievePerson = "洋码头";
			coabean.setReceievePerson(receievePerson);

			coabean.setUserId(288484);
			coabean.setUserType(UserTypeEnum.Seller);
			// 发送请求
			coacall.setData(coabean);
			coacall.callService();
			Logger.verifyEquals(ECode.ERROR400, coacall.getCode(), "验证Code");
			Logger.verifyEquals("orderId:订单号不能为空", coacall.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ChangeOrderAddress_009() {
		Logger.start(false, "操作用户id为null,修改订单地址失败");
		try {
			// 设置参数
			String address = "南京路步行街";
			coabean.setAddress(address);

			long orderId = 132314214;
			coabean.setOrderId(orderId);
			String phone = "13200000005";
			coabean.setPhone(phone);
			String postCode = "986574";
			coabean.setPostCode(postCode);

			String receievePerson = "洋码头";
			coabean.setReceievePerson(receievePerson);

			//coabean.setUserId(288484);
			coabean.setUserType(UserTypeEnum.Seller);
			// 发送请求
			coacall.setData(coabean);
			coacall.callService();
			Logger.verifyEquals(ECode.ERROR400, coacall.getCode(), "验证Code");
			Logger.verifyEquals("userId:操作用户Id未提供", coacall.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_ChangeOrderAddress_010() {
		Logger.start(false, "收货人地址为null,修改订单地址失败");
		try {
			// 设置参数
			//coabean.setAddress(address);

			long orderId = 132314214;
			coabean.setOrderId(orderId);
			String phone = "13200000005";
			coabean.setPhone(phone);
			String postCode = "986574";
			coabean.setPostCode(postCode);

			String receievePerson = "洋码头";
			coabean.setReceievePerson(receievePerson);

			coabean.setUserId(288484);
			coabean.setUserType(UserTypeEnum.Seller);
			// 发送请求
			coacall.setData(coabean);
			coacall.callService();
			Logger.verifyEquals(ECode.ERROR400, coacall.getCode(), "验证Code");
			Logger.verifyIsContains(ECode.ERROR400_MSG_36, coacall.getMsg(),
					"验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	// 修改地址后，无需更改税费和税率
	public void Tc_ChangeOrderAddress_011() {
		Logger.start(false, "收货人null,修改订单地址失败");
		try {
			// 设置参数
			String address = "南京路步行街";
			coabean.setAddress(address);

			long orderId = 132314214;
			coabean.setOrderId(orderId);
			String phone = "13200000005";
			coabean.setPhone(phone);
			String postCode = "986574";
			coabean.setPostCode(postCode);

			//coabean.setReceievePerson(receievePerson);

			coabean.setUserId(288484);
			coabean.setUserType(UserTypeEnum.Seller);
			// 发送请求
			coacall.setData(coabean);
			coacall.callService();
			Logger.verifyEquals(ECode.ERROR400, coacall.getCode(), "验证Code");
			Logger.verifyEquals("receivePerson:收货人姓名不能为空", coacall.getMsg(),
					"验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ChangeOrderAddress_012() {
		Logger.start(true, "订单状态为已接单，店主修改收货人地址成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");


			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 0, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder1(productBean, autotest_goodsOwnerId, 2);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long userId = placeOrderResp.getUserId();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			SQPayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId), placeOrderResp
							.getPayableAmount().subtract(BigDecimal.ZERO)
							.toString(), (int) userId,
					BigDecimal.ZERO.toPlainString());

			Thread.sleep(5000);
			
			SqTradingCallService.sellerAcceptOrder(orderId);

			Logger.comment("前置数据准备結束");

			OrderExtPo orderExtPo = orderWapper
					.selectOrderExtByOrderId(orderId);
			// 设置参数
			String address = "南京路步行街";
			coabean.setAddress(address);

			coabean.setOrderId(orderId);
			String phone = "13200000005";
			coabean.setPhone(phone);
			String postCode = "986574";
			coabean.setPostCode(postCode);

			String receievePerson = "洋码头";
			coabean.setReceievePerson(receievePerson);

			coabean.setUserId(productBean.getSellerId());
			coabean.setUserType(UserTypeEnum.Seller);
			// 发送请求
			coacall.setData(coabean);
			coacall.callService();
			Logger.verifyEquals(ECode.SUCCESS, coacall.getCode(), "验证Code");
			// 验证数据库
			verify(orderExtPo);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}